//
//  SISSNotificationListCell.m
//  SISSAPP
//
//  Created by 优房网 on 2017/6/20.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSNotificationListCell.h"
#import "SISSNotificationRecordModel.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface SISSNotificationListCell ()

/* bgView */
@property (weak, nonatomic) UIView *bgView;
/* topImageView */
@property (weak, nonatomic) UIImageView *topImageView;
/* titleLabel */
@property (weak, nonatomic) UILabel *titleLabel;
/* timeLabel */
@property (weak, nonatomic) UILabel *timeLabel;
/* lineView */
@property (weak, nonatomic) UIView *lineView;
/* checkLabel */
@property (weak, nonatomic) UILabel *checkLabel;
/* dirImageView */
@property (weak, nonatomic) UIImageView *dirImageView;

@end
@implementation SISSNotificationListCell

- (void)setNofiRecordModel:(SISSNotificationRecordModel *)nofiRecordModel {
    _nofiRecordModel = nofiRecordModel;
    
    [self.topImageView sd_setImageWithURL:[NSURL URLWithString:nofiRecordModel.image] placeholderImage:[UIImage imageNamed:@"图片加载占位符"]];
    
    self.titleLabel.text = nofiRecordModel.title;
    self.timeLabel.text = [nofiRecordModel.releaseTime substringToIndex:10];
    
    CGFloat topImageHeight = [nofiRecordModel.image substringFromIndex:BASE_URL.length].length > 0 ? 120 : 0.01;
    
    [self.topImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(topImageHeight);
    }];
    
    if (self.notificationType == SISSNotificationTypeSystemMessage && topImageHeight < 100) {
        self.lineView.hidden = YES;
        self.checkLabel.font = [UIFont systemFontOfSize:16];
        self.checkLabel.text = nofiRecordModel.content;
        self.dirImageView.hidden = YES;
    } else {
        self.lineView.hidden = NO;
        self.checkLabel.font = [UIFont systemFontOfSize:12];
        self.dirImageView.hidden = NO;
    }
}


#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    self.contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    
    // 视图
    UIImageView *topImageView = [[UIImageView alloc] init];
    topImageView.contentMode = UIViewContentModeScaleAspectFill;
    topImageView.layer.masksToBounds = YES;
    [bgView addSubview:topImageView];
    self.topImageView = topImageView;
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = COLOR(0x334A60);
    titleLabel.text = @" ";
    [bgView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    // 时间
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = [UIFont systemFontOfSize:16];
    timeLabel.textColor = COLOR(0x334A60);
    timeLabel.text = @" ";
    [bgView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    // 底部线条
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:lineView];
    self.lineView = lineView;
    
    // 查看详情label
    UILabel *checkLabel = [[UILabel alloc] init];
    checkLabel.font = [UIFont systemFontOfSize:12];
    checkLabel.textColor = COLOR(0x334A60);
    checkLabel.text = @"点击查看详情";
    checkLabel.numberOfLines = 0;
    [bgView addSubview:checkLabel];
    self.checkLabel = checkLabel;
    
    // 箭头按钮
    UIImageView *dirImageView = [[UIImageView alloc] init];
    dirImageView.image = [UIImage imageNamed:@"btn_list_more"];
    [bgView addSubview:dirImageView];
    self.dirImageView = dirImageView;

}


/**
 设置约束
 */
- (void)initConstraint {
    [super initConstraint];
    
    CGFloat margin = 8.0;
    WeakSelf
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(margin);
        make.right.equalTo(weakSelf.contentView).offset(-margin);
        make.top.bottom.equalTo(weakSelf.contentView);
    }];
    
    [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf.bgView).offset(margin * 0.5);
        make.right.equalTo(weakSelf.bgView).offset(- margin * 0.5);
        make.height.mas_equalTo(11 * margin);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.topImageView.mas_bottom).offset(2 * margin);
        make.left.equalTo(weakSelf.bgView).offset(2 * margin);
        make.right.equalTo(weakSelf.timeLabel.mas_left).offset(- margin * 0.5).priority(999);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.titleLabel);
        make.right.equalTo(weakSelf.bgView).offset(- margin);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.topImageView);
        make.right.equalTo(weakSelf.bgView).offset(-0.5 * margin);
        make.top.equalTo(weakSelf.titleLabel.mas_bottom).offset(margin);
        make.height.mas_equalTo(1);
    }];
    
    [self.checkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLabel);
        make.top.equalTo(weakSelf.lineView.mas_bottom).offset(margin);
        make.right.equalTo(weakSelf.bgView).offset(-margin);
        make.bottom.equalTo(weakSelf.bgView).offset(-margin);
    }];
    
    [self.dirImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.checkLabel);
        make.right.equalTo(weakSelf.bgView).offset(-margin);
    }];
}

@end
